package com.mcy.springbatch.job.nestjob;

import com.mcy.springbatch.job.BaseJobConfig;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.builder.JobStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Job嵌套执行，ParentJob包含ChildJob1和ChildJob2
 * 其实就是把子job转为step返回给父job调用
 */
@Configuration
public class ParentJobDemoConfig extends BaseJobConfig {

    @Autowired
    private Job childJobOne;

    @Autowired
    private Job childJobTwo;

    @Bean
    public Job parentJobParent(){
        return jobBuilderFactory.get("parentJobParent")
                .incrementer(new RunIdIncrementer())
                .start(childJob1())
                .next(childJob2())
                .build();
    }

    /**
     * 使用JobStepBuilder把ChildJob的Job转化成Step返回
     * @return
     */
    public Step childJob1(){
        return new JobStepBuilder(new StepBuilder("childJob1childJob1Step"))
                .job(childJobOne)
//                .launcher(jobLauncher)
                .repository(jobRepository)
                .transactionManager(platformTransactionManager)
                .build();
    }

    public Step childJob2(){
        return new JobStepBuilder(new StepBuilder("childJob2childJob2Step"))
                .job(childJobTwo)
//                .launcher(jobLauncher)
                .repository(jobRepository)
                .transactionManager(platformTransactionManager)
                .build();
    }

}
